var mytable;
$(function () {
    var obj = $('#seeNumber').val();
    var userId = $('#userId').val();
    var edit = $('#edit').val();
    var update = $('#update').val();
    var superStr = $('#super').val();
    var state = 0;
    if(obj){
        state = 1;
    }else{
        state = 0;
    }
     mytable = $('#myTable').dataTableExtend({
        "ajax": {
            "url": sysInfo.basePath + "/zs/talent/talent_list.do",
            "type": "post"
        }, "fnServerParams": function (aoData) {
            aoData = $.extend(aoData, $('.titleSearch').serialize2object());
            return aoData;
        },
        "pageLength": 10,//每页显示的条数默认是10条，有特殊业务需要显示不同条数的列表才应该加入该参数
        "scrollX": true,//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 3,//左侧锁定2列
            "rightColumns": 2//右侧锁定1列
        },
        "aoColumns": [
            {
                "sClass": "check-th",
                "title": "<input type=\"checkbox\" class=\"checkall\" value=\"undefined\">",
                "orderable": false,
                "data": "id",
                "render": function (data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                }
            },
            {
                "title": "序号",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return meta.row + 1;
                }
            },
            {
                "title": "姓名",
                "data": "talentName",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, full, meta) {
                    var html = '<div class="line2" style="min-width: 50px" title="'+data+'">'+data+'</div> ' + '<p>' + emptyStringConvert(full.talentPhone) + '</p>'
                    if(state){
                        html+= '<a href="javascript:void(0)" class="opt-icon look" onclick="seeNumber(this,\''+full.id+'\')"></a>';
                    }
                    return html;
                }
            },
            {
                "title": "性别",
                "data": "talentSex",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return data;
                }
            },

            {
                "title": "年龄",
                "data": "talentAge",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            },
            {
                "title": "学历",
                "data": "education",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            },
            {
                "title": "毕业院校",
                "data": "school",
                "orderable": false,
                "width":"50px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 40px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "专业",
                "data": "major",
                "orderable": false,
                "width":"50px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 50px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "当前职位",
                "data": "currentPosition",
                "orderable": false
            },
            {
                "title": "在职公司",
                "data": "currentCompany",
                "orderable": false,
                "width":"150px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "所持证书",
                "data": "certificate",
                "orderable": false,
                "width":"120px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "目标行业",
                "data": "targetIndustry",
                "orderable": false,
                "width":"80px",
                "render":function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 50px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "期望薪资",
                "data": "salaryMin",
                "orderable": false,
                "width":"50px",
                "render": function (data, type, row, meta) {
                    var str = "";
                    str +=  data ? data +"-" :"";
                    if(row.salaryMax){
                        str += row.salaryMax+"K";
                    }
                   return str;
                }
            }
            ,
            {
                "title": "地区",
                "data": "jobArea",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 50px" title="'+data+'">'+data+'</div> ';
                }
            }
            ,
            {
                "title": "求职状态",
                "data": "jobStatus",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            },
            {
                "title": "人才状态",
                "data": "talentStatus",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            }
            ,
            {
                "title": "录入人",
                "data": "createrName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            }
            ,
            {
                "title": "匹配人",
                "data": "matchUserName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data ? data : "";
                }
            }
            ,
            {
                "title": "简历附件",
                "data": "files",
                "orderable": false,
                "width":"120px",
                "render": function (data, type, row, meta) {
                    return '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ';
                }
            },
            {
                "title": "操作",
                "orderable": false,
                "data": "id",
                "render": function (d, type, r, meta) {
                    var result ="";
                    //打电话
                    if((userId == r.createrId || edit==1) && r.talentStatus=="未匹配" ) {
                        result += '<a href=\'javascript:void(0)\' ti  onclick="editTalent(\'' + r.id + '\')" /><span class="layui-table-link">编辑</a>&nbsp;';
                    }
                    if(r.talentStatus=="未匹配" && update==1 ) {
                        result += '<a href=\'javascript:void(0)\' ti  onclick="updateTalent(\'' + r.id + '\')" /><span class="layui-table-link">状态更新</a>';
                    }else if(r.talentStatus !="未匹配" && (superStr==1 || userId == r.matchUserId)){
                        result += '<a href=\'javascript:void(0)\' ti  onclick="updateTalent(\'' + r.id + '\')" /><span class="layui-table-link">状态更新</a>';
                    }
                    if(r.talentStatus=="未匹配"){
                        result += '&nbsp<a href=\'javascript:void(0)\' ti  onclick="talentRecord(\'' + r.id + '\')" /><span class="layui-table-link">跟踪备注</a>';
                    }else if(r.talentStatus !="未匹配" && (superStr==1 || userId == r.matchUserId)){
                        result += '&nbsp<a href=\'javascript:void(0)\' ti  onclick="talentRecord(\'' + r.id + '\')" /><span class="layui-table-link">跟踪备注</a>';
                    }
                    return result;
                }
            }
        ],
    });
});

/**
 *调新建页面
 */
function create(){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/talent/creatTalent.html", function (data) {
        dgg.Open({
            title: '新增人才',
            content: data,
            area: '500px',
            btn: ["确定", "取消"],
            yes: function () {
                talentCreate();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');

}
function editTalent(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/talent/editTalent.html",{id:id}, function (data) {
        dgg.Open({
            title: '编辑人才',
            content: data,
            area: '500px',
            btn: ["确定", "取消"],
            yes: function () {
                saveTalent();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');
}
function saveTalent(){
    var talentName = $('[name="talentName"]').val();
    var talentAge = $('[name="talentAge"]').val();
    var salaryMin  = $('[name="salaryMin"]').val();
    var salaryMax = $('[name="salaryMax"]').val();
    var talentPhone = $('[name="talentPhone"]').val();
    var oldPhone = $('[name="oldPhone"]').val();

    if(isEmpty(talentName)){
        DGG.Warning('姓名不能为空！');
        return;
    }
    if(isEmpty(talentPhone)){
        DGG.Warning('联系电话不能为空！');
        return;
    }
    if(!isEmpty(talentAge)){
        if(!isInterval($("#talentAge"),100,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin)){
        if(!isInterval($("#salaryMin"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMax)){
        if(!isInterval($("#salaryMax"),5000,1,true)){
            return;
        };
    }
    if(oldPhone != talentPhone){
        if(!isTel($("#talentPhone"),true)){
            return;
        }
    }
    if(!isEmpty(salaryMax) && !isEmpty(salaryMin)){
        if(salaryMin*1>salaryMax*1){
            DGG.Warning('期望薪资后面的值要大于前面的值！');
            return;
        }
    }

    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定编辑人才库？", '编辑人才库', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/talent/saveTalent.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}
function talentCreate(){
    var talentName = $('[name="talentName"]').val();
    var talentAge = $('[name="talentAge"]').val();
    var salaryMin  = $('[name="salaryMin"]').val();
    var salaryMax = $('[name="salaryMax"]').val();
    var talentPhone = $('[name="talentPhone"]').val();

    if(isEmpty(talentName)){
        DGG.Warning('姓名不能为空！');
        return;
    }
    if(isEmpty(talentPhone)){
        DGG.Warning('联系电话不能为空！');
        return;
    }
    if(!isTel($("#talentPhone"),true)){
        return;
    }
    if(!isEmpty(talentAge)){
        if(!isInterval($("#talentAge"),100,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin)){
        if(!isInterval($("#salaryMin"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMax)){
        if(!isInterval($("#salaryMax"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMax) && !isEmpty(salaryMin)){
        if(salaryMin*1>salaryMax*1){
            DGG.Warning('期望薪资后面的值要大于前面的值！');
            return;
        }
    }

    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定新增人才库？", '新增人才库', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/talent/addTalent.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}

function reloadTable1() {
    mytable.ajax.reload();
}
function emptyStringConvert(str) {
    if (null == str || '' == str) {
        return '';
    } else {
        return str;
    }
}

/**
 * c
 * @param obj
 * @param number
 * @param type
 * @param getType
 */
function seeNumber(obj,id) {
    $.ajax({
        url: sysInfo.basePath + "/zs/talent/see_number.do",
        data: {id:id},
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                console.log($(obj));
                var $obj = $(obj).prev();

                var beforeNumber = $obj.html();
                var number = result.data;
                $obj.html(number);
                $(obj).hide();
                DGG.Layer.message("5秒后自动隐藏", {icon: '2'});
                window.setTimeout(function () {
                    $obj.html(beforeNumber);
                    $(obj).show();
                }, 5000);
                DGG.Layer.message("5秒后自动隐藏", {icon: '2'});
            } else {
                DGG.Layer.message(result.msg, {icon: '2'});
            }
        }
    });
}
// 打电话
function call() {
    var dataArr = $('#myTable').dataTableExtend('getCheck');
    if (dataArr.length == 0) {
        DGG.Warning('请选择一条订单')
        return false;
    }
    if (dataArr.length > 1) {
        DGG.Warning('只能选择一条订单');
        return false;
    }
    callPhoneByTalent(dataArr[0].id);
}
function callPhoneByTalent(id) {
    if (!id) {
        DGG.Warning('ID为空！');
        return;
    }
    DGG.loading();
    $.ajax({
        url: sysInfo.basePath + "/zs/talent/find_talent.do",
        data: {id: id},
        type: "POST",
        dataType: "json",
        success: function (result) {
            DGG.loading('close');
            if (result.code == 0) {
                var talentInfo = result.data;
                if (talentInfo) {
                    callPhone(talentInfo.id,talentInfo.name,talentInfo.phone,talentInfo.phoneStr,'');
                } else {
                    DGG.Errors("人才号码不可拨打!");
                }
            } else {
                DGG.Errors(result.msg);
            }
        }
    });
}
function downFile(){
    var dataArr = $('#myTable').dataTableExtend('getCheck');
    console.log(dataArr);
    if (dataArr.length == 0) {
        DGG.Warning('请选择一条订单')
        return false;
    }
    if (dataArr.length > 1) {
        DGG.Warning('只能选择一条订单');
        return false;
    }
    if(dataArr[0].talentStatus !="未匹配" ){
        DGG.Warning('人才状态在未匹配才能下载！');
        return false;
    }
    if(dataArr[0].files) {
        downLoadPage(dataArr[0].id);
    }else{
        DGG.Warning('没有简历附件！');
        return false;
    }
}
function downLoadPage(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/talent/downLoadFile.html",{id:id}, function (data) {
        dgg.Open({
            title: '简历下载',
            content: data,
            area: ['800px', '800px'],
            btn: ["取消"],
            btn2: function () {

            }
        });
    }, 'html');
    DGG.loading('close');
}
function updateTalent(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/talent/statePage.html",{id:id}, function (data) {
        dgg.Open({
            title: '更新状态',
            content: data,
            area: '500px',
            btn: ["确定", "取消"],
            yes: function () {
                update();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');
}
function update(){
    var talentStatus = $("#talentStatus").val();
    var matchCompanyId = $("#matchCompanyId").val();
    if(talentStatus == "TALENT_STATUS_03"){
        if(isEmpty(matchCompanyId)){
            DGG.Warning('请选择企业！');
            return;
        }
    }
    var talentStausName = $("#talentStatus").find("option:selected").text();
    var params=$('#flowCreate').serializeArray();
    params.push({name:'talentStausName',value:talentStausName});
    DGG.Confire("确定更新状态？", '更新状态', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/talent/update.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！');
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                   reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })

}

function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}
//导出办理中数据
function exportIprocessData() {
    var paras = $('.titleSearch').serialize2object();
    if (Object.keys(paras).length == 0) {
        DGG.Warning('请加入搜索条件后导出！');
        return;
    }
    $.extend(paras, {type: $(".title-tab").find("a.active").attr("data-status")});
    exportDataAbandoned('/zs/talent/export_talent.do', paras);
}
//跟踪备注
function talentRecord(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/talent/recordPage.html",{id:id}, function (data) {
        dgg.Open({
            title: '跟踪备注',
            content: data,
            area: ['800px','800px'],
            btn: ["确定", "取消"],
            yes: function () {
                saveRecord();
            }, btn2: function () {
                DGG.loading("close");
            }
        });
    }, 'html');
}
function saveRecord(){
    var text = $("#remark").val();
    var talentId = $("#talentId").val();
    if(text.length==0){
        DGG.Warning("备注不能为空");
        return;
    }
    DGG.loading();
    $.ajax({
        type: 'POST',
        async: true,
        data: {'talentId':talentId,'text':text},
        url: sysInfo.basePath + '/zs/talent/saveRecord.do',
        dataType: 'JSON',
        success: function (data) {
            DGG.loading('close');
            if (data.code == 0) {
                DGG.Succeed('操作成功！')
                reloadRecordTable();
                $("#remark").val("");
            } else {
                DGG.Errors(data.msg);
            }
        },
        error: function () {
            DGG.Errors('保存失败，请联系管理员！！');
        }
    })

}
function reloadRecordTable(){
    $('#talentRecord').dataTableExtend("reload");
}